Adding some more judges, here and there.
[and.git] / lib / Mi manual de algoritmos / version_maraton_interuniversitaria_2008-2 / src / geometria / distance_point_to_segment.cpp
blob072ee80dfe7d19b7e941adfcd59c1912a6fe5676
1 struct point{
2 double x,y;
3 };
5 inline double dist(const point &a, const point &b){
6 return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
9 inline double distsqr(const point &a, const point &b){
10 return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
14 Returns the closest distance between point pnt and the segment that goes from point a to b
15 Idea by: http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/
17 double distance_point_to_segment(const point &a, const point &b, const point &pnt){
18 double u = ((pnt.x - a.x)*(b.x - a.x) + (pnt.y - a.y)*(b.y - a.y)) / distsqr(a, b);
19 point intersection;
20 intersection.x = a.x + u*(b.x - a.x);
21 intersection.y = a.y + u*(b.y - a.y);
22 if (u < 0.0 || u > 1.0){
23 return min(dist(a, pnt), dist(b, pnt));
25 return dist(pnt, intersection);